package com.nbjtjc.safe.activiti.listener;

import com.alibaba.druid.pool.DruidDataSource;
import com.nbjtjc.safe.activiti.mapper.ExtActNodeMapper;
import com.nbjtjc.safe.activiti.model.ExtActNode;
import com.nbjtjc.safe.mapper.emg.EmgPlanPublicMapper;
import com.nbjtjc.safe.service.emg.EmgPlanPublicService;
import com.nbjtjc.safe.util.SpringContextUtils;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @auther guqianbin
 * @date 2018/5/29 16:55
 **/
@Component("myExecutionListener")
public class MyExecutionListener implements ExecutionListener, TaskListener, Serializable {


    @Value("${emgPlanPublic.finishValue}")
    private Integer finishValue;

    private ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<>();


    @Override
    public void notify(DelegateExecution delegateExecution) {
        DruidDataSource druidDataSource = SpringContextUtils.getBean(DruidDataSource.class);
        System.out.println(delegateExecution.getVariable("busKey"));
        EmgPlanPublicMapper emgPlanPublicMapper = SpringContextUtils.getBean(EmgPlanPublicMapper.class);
        if ("end".equals(delegateExecution.getEventName())) {
            Integer planPublicId = (Integer) delegateExecution.getVariable("busKey");
            emgPlanPublicMapper.finish(planPublicId,finishValue);
        }
    }

    @Override
    public void notify(DelegateTask delegateTask) {
        DruidDataSource druidDataSource = SpringContextUtils.getBean(DruidDataSource.class);
        ExtActNodeMapper extActNodeMapper = SpringContextUtils.getBean(ExtActNodeMapper.class);
        EmgPlanPublicMapper emgPlanPublicMapper = SpringContextUtils.getBean(EmgPlanPublicMapper.class);
        EmgPlanPublicService emgPlanPublicService=SpringContextUtils.getBean(EmgPlanPublicService.class);
        System.out.println(delegateTask.getEventName());
        if (delegateTask.getEventName().equals("create")) {

            Integer planPublicId = (Integer) delegateTask.getVariable("busKey");
            int status = 0;
            ExtActNode extActNode = extActNodeMapper.findByProcdefIdAndNodeId(delegateTask.getProcessDefinitionId(), delegateTask.getTaskDefinitionKey());
//            emgPlanPublicMapper.updateStatus(planPublicId, extActNode.getStatus());
            emgPlanPublicMapper.updateStatusByStartPlanId(planPublicId, extActNode.getStatus());
            if(extActNode.getPublicNotice()){
                emgPlanPublicService.updateEmgPlanPublicNoticeStatus(planPublicId);
            }
        }
    }
}
